<template>
    <cvueDialog
       :dialogVisible='dialogVisible'
        title='选择学校'
        :isShowFoot='true'
        dialogWidth='60%'
        @handleOpen='getDetail'
        @closeDialog='closeDialog'
        @confirmDialog='confirmDialog'
        @handleClose='closeDialog'
    >
        <div slot="dialogBody">
            <div style="line-height: 31px;">
                <area-component
                  @areaObj='areaObj'
                  :province="sheng"
                  :city="shi"
                  :county="qu"
                ></area-component>
                <el-form inline label-width="80px" style="display: inline-block;">
                  <el-form-item label="学段">
                    <el-select v-model="phaseStudyId" placeholder="请选择学段">
                      <el-option value="" label="全部">全部</el-option>
                        <el-option
                        v-for="item in stageData"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                        </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="学校名称">
                      <el-input v-model.trim="schoolName" placeholder="请输入学校名称"></el-input>
                  </el-form-item>
                </el-form>
                <el-button  type="primary" icon="el-icon-search" @click="searchList(1)">查询</el-button>
              
            </div>
            <div class="right-main">
            <cvue-table
              :option="tableOption"
              :data="tableData"
              ref="tbl1"
              :isShowAdd="false"
              :page="page"
              :tablePage="tablePage"
              :pageSize="page.pageSize"
              :loading="tableLoading"
              @current-change="handleCurrentChange"
              @size-change="handleSizeChange"
              @cellClick='handleClick'
              :isShowPage="true"
            >
              <template slot-scope="scope" slot="openInfo">
                <span @click="handleOpenList(scope.row)">{{scope.row.openInfo}}</span>
              </template>
            
            </cvue-table>
          </div>
        </div>
    </cvueDialog>
</template>
<script>
import cvueTable from '@/components/cvue-table'
import cvueDialog from '@/components/cvue-dialog'
import areaComponent from '@/components/areaComponent'
import {baseUrl} from '@/config/config'
export default {
    name: 'schoolList',
    components: {
       cvueTable,
       cvueDialog,
       areaComponent
    },
    props: {
      dialogVisible: {
        type: Boolean,
        default: false
      }
    },
    data () {
      return {
        sheng: '',
        shi: '',
        qu: '',
        phaseStudyId: '',
        schoolName: '',
        stageData: [],
      
         tablePage: 1,
         tableLoading: false,
        page: {
            total: 0, // 总页数
            currentPage: 1, // 当前页数
            pageSize: 10, // 每页显示多少条
            pageSizes: [10, 20, 30, 40, 50]
        },
        tableOption: {
            stripe: false,
            width: '100%',
            border: false,
            index: false,
            cloumn: [
            { prop: 'name', label: '学校名称', align: 'center', width: 150 },
            { prop: 'stageName', label: '所属学段', align: 'center' },
            { prop: 'adress', label: '所属地区', align: 'center' },
            { prop: 'agentName', label: '代理商', align: 'center' },
            { prop: 'adminLogin', label: '校管总账号', align: 'center' }
            ]
        },
        tableData: [],
        areaData: '',
        areaId: '',

        school: ''
      }
    },
    mounted () {
    
    },
    methods: {
        getDetail () {
            this.getSchoolList()
            this.getStage()
        },
        closeDialog () {
          this.$emit('close')
        },
        confirmDialog () {
          console.log(this.school)
          this.$store.state.common.cookiesObj.schoolId = this.school.id
          this.$store.state.common.cookiesObj.schoolName = this.school.name
          this.$store.state.common.cookiesObj = JSON.parse(JSON.stringify(this.$store.state.common.cookiesObj))
          this.$emit('confirm')
          this.closeDialog()
        },
        handleClick (row, index, prop) {
          this.school = row
        },
            // 改变pagesize
        handleSizeChange (val) {
        this.page.pageSize = val
        this.getSchoolList()
        },
        // 改变页码
        handleCurrentChange (val) {
        this.tablePage = val
        this.getSchoolList()
        },
        // 获取地区的数据
        areaObj (data) {
          this.areaData = data
        },
         // 获取学段
        getStage () {
          var params = {
            // id: this.$store.state.common.cookiesObj.schoolId,
            PageNum: 1,
            PageSize: 100,
            Sorting: 'orderNum ASC'
          }
          this.$store.dispatch('schoolManage/Stage', params).then(res => {
              this.stageData = res.items
              this.$store.state.schoolManage.stageData = res.items
          })
        },
        // 获取学年
        getSchoolYear () {
          var params = {
            // id: this.$store.state.common.cookiesObj.schoolId,
            PageNum: 1,
            PageSize: 100,
            Sorting: 'name DESC'
          }
          this.$store.dispatch('schoolManage/SchoolYear', params).then(res => {
              this.$store.state.schoolManage.schoolYearData = res.items
          })
        },
        // 获取代理商
        getAgents () {
          this.$store.dispatch('schoolManage/Agents').then(res => {
              this.$store.state.schoolManage.agentData = res
          })
        },
        // 获取字典数据
       async getZidian () {
          for (var obj in this.dict) {
            var params = {
                typeCode: obj
              }
            var dictItem = await this.$store.dispatch('schoolManage/Dictionaryvalue', params)
            // console.log(dictItem)
           this.dict[obj] = dictItem
           this.$store.state.schoolManage.dict = this.dict
          }
        },
        // 获取年级
        getGrades () {
          var params = {
            // schoolId: this.$store.state.common.cookiesObj.schoolId
            Sorting: 'orderNum ASC'
          }
          this.$store.dispatch('schoolManage/GradesBySchoolId', params).then(res => {
              this.$store.state.schoolManage.gradeData = res.items
          })
        },
        // 搜索按钮
        searchList (num) {
          if (this.areaData.countyId) {
              this.areaId = this.areaData.countyId
           } else if (this.areaData.cityId) {
              this.areaId = this.areaData.cityId
           } else {
             this.areaId = this.areaData.provinceId
           }
            this.tablePage = 1
            this.getSchoolList() 
        },
       
        // 获取学校列表
        getSchoolList () {
          // if (!this.areaId || !this.phaseStudyId) {
          //     return
          // }
           var params = {
              PageNum: this.tablePage,
              PageSize: this.page.pageSize,
              AreaId: this.areaId,
              StageId: this.phaseStudyId
            }
            if (this.schoolName) {
              params.SchoolName = this.schoolName
            }
            
           this.$store.dispatch('schoolManage/GetSchoolList', params).then(res => {
              // this.areaData = ''
              this.tableData = res.items
              this.page.total = res.totalCount
              this.page.currentPage = this.tablePage
          })   
        }
    }
}
</script>
<style scoped>
.right-main{
    margin-top: 15px;
    background: #fff;
    min-height: 150px;
    padding: 0 0 20px;
}
.operation span{
    cursor: pointer;
    color: #409EFF;
}
</style>
